package com.swgk.core.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {
    public static int time = 60 * 60 * 60;
    public static Calendar c;
    public static long endTime;
    public static Date date;
    public static long startTime;
    public static long midTime;

    /*
     * 将时间转换为时间戳  10位
     */
    public static long dateTo10Timestamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime() / 1000;
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }


    /*
     * 将时间转换为时间戳  13位
     */
    public static long dateTo10Timestamp3(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    /*
     * 将时间转换为时间戳  13位
     */
    public static long dateTo10Timesstamp13(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    /*
     * 将时间转换为时间戳  13位
     */
    public static long dateTo10Timestamp13(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    public static String date2Timestamp(long timestamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月");
        Date time = new Date(timestamp);
        return simpleDateFormat.format(time);
    }

    public static String date2Timestamp(Date timestamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月");
        return simpleDateFormat.format(timestamp);
    }

    public static String[] timestamp2DateNumber(Date timestamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        return simpleDateFormat.format(timestamp).split("-");
    }

    public static String[] timestamp2DateNumber(long timestamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date time = new Date(timestamp);
        return simpleDateFormat.format(time).split("-");
    }

    public static String stampToDay4(String s) {
        if (TextUtils.isEmpty(s) || s.equals("null")) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间转换为时间戳  10位
     */
    public static long dateTo10Timestamp2(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime() / 1000;
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    public static long timestampTo13(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(time);
        try {
            Date date = sdf.parse(sd);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            return 0;
        }

    }


    /*
     * 将时间转换为时间戳 13位
     */
    public static long dateTo13Timestamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    /*
     * 将时间转换为时间戳 13位
     */
    public static long dateTo13Timestamp2(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    /*
     * 将时间转换为时间戳 13位
     */
    public static long dateTo13Timestamp3(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    /*
     * 将时间转换为时间戳 13位
     */
    public static long dateTo13Timestam4(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    //将时间戳转换为时间 10位
    public static String timestampToDate2(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    //将时间戳转换为时间 13位
    public static String timestampTo13Date2(long time) {
        if (time < 10000000000L) {
            time = time;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    //将时间戳转换为时间 10位
    public static String timestampToDate(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    //将时间戳转换为时间 13位
    public static String timestampTo13Date3(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    //将时间戳转换为包含秒的时间，若时间为0(null)，则返回斜杠
    public static String timestampToDateSecond1(long time, String defaultText) {
        if (time <= 0) {
            return defaultText;
        }
        return timestampToDateSecond(time);
    }

    //将时间戳转换为包含秒的时间
    public static String timestampToDateSecond(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    //将时间戳转换为时间 13位   .
    public static String timestampTo13Date3Point(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    //将时间戳转换为时间 13位   .
    public static String timestampTo13DateNos(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    //将时间戳转换为时间 13位   .
    public static String timestampTo13Date3NoHours(long time) {
        if (time < 10000000000L) {
            time = time * 1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));
        return sd;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        String res;
        if (TextUtils.isEmpty(s))
            return "";
        if (s.contains("-") || s.contains(":")) {
            return s;
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long lt = new Long(s);
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
            return res;
        }
    }

    public static String getDate() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(d);
        return str;
    }

    public static String getDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str = sdf.format(date);
        return str;
    }

    public static String stampToDay(String s) {
        if (TextUtils.isEmpty(s)) return "";
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToDayName(String s) {
        if (TextUtils.isEmpty(s)) return "";
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间转换为时间戳 13位
     */
    public static String stampToMinute(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        return simpleDateFormat.format(date);

    }

    public static String stampToDay5(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToDay6(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToDay4(Long s) {
        if (s == null) {
            return "";
        }
        if (s == 0) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd");
        long lt = s;
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToDay5(Long s) {
        if (s == null) {
            return "";
        }
        if (s == 0) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToDay6(Long s) {
        if (s == null) {
            return "";
        }
        if (s == 0) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToMonthDay(String s) {
        if (s == null || s.equals("")) {
            return "";
        }
        String res = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = simpleDateFormat1.parse(s);
            res = simpleDateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return res;
    }

    public static String stampToDay2(String s) {
        String res = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat1.parse(s);
            res = simpleDateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 格式化日期根据子订单格式化样式
     *
     * @param dateStr
     * @param oldFormat
     * @param newFormat
     * @return
     */
    public static String formatDateForCustom(String dateStr, String oldFormat, String newFormat) {
        if (null == dateStr || dateStr.length() == 0) {
            return "/";
        }
        try {
            SimpleDateFormat format1 = new SimpleDateFormat(oldFormat);
            SimpleDateFormat format2 = new SimpleDateFormat(newFormat);
            Date d = format1.parse(dateStr);
            return format2.format(d);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * 将毫秒转时分秒
     *
     * @param time
     * @return
     */
    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        return String.format("%02d:%02d", minutes, seconds);
    }

    //分享-当年只显示月日，非当年显示年月日,当日显示时间
    public static String getDayTimeUtils(long createTime) {
        String dataTime = "";
        //返回时间的年-月-日
        String returnTime = timestampToDate(createTime);
        //系统时间的年-月-日
        String currentTime = timestampToDate(System.currentTimeMillis());
        //判断是否是相同的年
        if (getYears(createTime).equals(getYears(System.currentTimeMillis()))) {
            //判断系统年月日与返回的年月日是否相等
            if (currentTime.equals(returnTime)) {
                //相等展示时分
                dataTime = timestampTo13Date2(createTime);
            } else {
                //不相等展示月日
                dataTime = getMonthDay(createTime);
            }
        } else {
            dataTime = timestampToDate(createTime);
        }
        return dataTime;
    }

    //分享时间处理方法
    public static String getDayUtils(long createTime) {
        if (System.currentTimeMillis() > createTime) {
            String dataTime = "";
            //返回时间的年-月-日
            String returnTime = timestampToDate(createTime);
            //系统时间的年-月-日
            String currentTime = timestampToDate(System.currentTimeMillis());
            //判断是否是相同的年
            if (getYears(createTime).equals(getYears(System.currentTimeMillis()))) {
                //等展示月日
                dataTime = getMonthDay(createTime);
            } else {
                //
                dataTime = timestampToDate(createTime);
            }
            return dataTime;
        } else {
            return "";
        }
    }

    //把毫秒值转换成日期
    private static String getMonthDay(long createTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String format = simpleDateFormat.format(new Date(createTime));
        return format;
    }

    //把毫秒值转换成年
    private static String getYears(long createTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String format = simpleDateFormat.format(new Date(createTime));
        return format;
    }

    /**
     * 将毫秒转化成固定格式的时间
     * 时间格式: yyyy-MM-dd-HHmmssSSS
     *
     * @param millisecond 时间毫秒
     * @return 时间格式: yyyy-MM-dd-HHmmssSSS
     */
    public static String getDateTimeFromMillisecond(Long millisecond) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HHmmssSSS");
        Date date = new Date(millisecond);
        return simpleDateFormat.format(date);
    }


    /**
     * 计算时间差
     *
     * @param endTime
     * @return
     */
    public static String dateDiff(String endTime) {
        String strTime = null;
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = sd.format(curDate);
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(str).getTime();
            day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            // 输出结果
          /*  if (day >= 1) {
                strTime = day + "天" + hour + "时";
            } else {

                if (hour >= 1) {
                    strTime = day + "天" + hour + "时" + min + "分";

                } else {
                    if (sec >= 1) {
                        strTime = day + "天" + hour + "时" + min + "分" + sec + "秒";
                    }
                }
            }*/
            strTime = day + "天" + (hour == 0 ? 0 : String.format("%02d", hour)) + "时" + (min == 0 ? 0 : String.format("%02d", min)) + "分" + sec + "秒";
            return strTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }


    public static String stampToDatePick(String s) {
        String res;
        if (null == s)
            return "";
        if (s.contains("/") || s.contains(":")) {
            return s;
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            long lt = new Long(s);
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
            return res;
        }
    }

    public static String stampToDate(String dataFormat, long time) {
        String res = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dataFormat);
            Date date = new Date(time);
            res = simpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 获取指定日期最开始时间
     *
     * @param day  负数：当前日期前；正数：当前日期后
     * @param time 时间戳
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getCustomizeDayMorning(int day, long time) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat("yyyyMMdd").parse(stampToDate("yyyyMMdd", time)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.add(Calendar.DATE, day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取指定日期最后时间
     *
     * @param day  负数：当前日期前；正数：当前日期后
     * @param time 时间戳
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getCustomizeDayNight(int day, long time) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(new SimpleDateFormat("yyyyMMdd").parse(stampToDate("yyyyMMdd", time)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.add(Calendar.DATE, day);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    public static String CurrentToTime() {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        res = simpleDateFormat.format(date);
        return res;
    }

    public static String stampToTime(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 格式化时间戳
     *
     * @param date
     * @return formatStr 自定义格式化样式：yyyy-MM-dd HH:mm:ss
     */
    public static String formatDate(String date, String formatStr) {
        long time = 0L;
        try {
            time = Long.parseLong(date);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return formatDate(time, formatStr);
    }

    public static String formatDate(long date, String formatStr) {
        if (date == 0) return "";
        //需要其他格式的以此类推
        if (TextUtils.isEmpty(formatStr)) {
            formatStr = "yyyy-MM-dd HH:mm:ss";
        }
        String dateStr = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            dateStr = format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    public static String getTimeStamp(String time, String type) {
        SimpleDateFormat sdf = new SimpleDateFormat(type, Locale.CANADA);
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return String.valueOf(date.getTime());
    }
}
